package com.yusong.evenserialportdemo

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var serialControl = SerialControl()
        serialControl.open("ttyS1",2400.toString(),8,1,'E')

        et.setText("68AAAAAAAAAAAA68110433333333AD16")
        btn_send.setOnClickListener {
            var text = et.text.toString()
            if (!TextUtils.isEmpty(text)) {
              serialControl.sendCommand(text)
            }
        }

    }

    inner class SerialControl() : SerialPortManager() {
        var sMsg = StringBuilder()
        override fun onDataReceived(received: ByteArray?, size: Int) {
            val hexStr = ByteUtil.bytes2HexStr(received, 0, size)
            sMsg.append(hexStr)
            Log.d("feisher","接收的数据===》" + sMsg.toString())

            tv_response.text =  sMsg.toString()+""
        }
    }
}
